package com.hero.updown;

import android.content.Context;
import android.os.Environment;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.convert.FileConvert;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.io.File;

public class DownUtils {

    private Context context;

    public DownUtils(Context context){
        this.context = context;
    }


    public void down(String url,String path) {
        down(url,path,null);
    }

    public void down(String url,String path,StatusCallback callback) {
        ProgressBarDialog dialog = new ProgressBarDialog(context,callback,1);
        OkGo.<File>get(url)
                .execute(new FileCallback(path,"") {

                    @Override
                    public void onStart(Request<File, ? extends Request> request) {
                        if (dialog != null){
                            dialog.show();
                            dialog.setText1(url);
                        }
                    }

                    @Override
                    public void onError(Response<File> response) {
                        if (dialog != null){
                            dialog.setClose();
                        }
                        Toast.makeText(context,"下载失败！",Toast.LENGTH_LONG).show();
                    }


                    @Override
                    public void onSuccess(Response<File> response) {
                        if (dialog != null){
                            dialog.setText2(response.body().getPath());
                            dialog.setClose();
                        }
                        Toast.makeText(context,"下载成功！",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void downloadProgress(Progress progress) {
                        if (dialog != null){
                            dialog.setProgress(progress.fraction);
                        }
                    }

                });
    }
}
